package stark.app.frame.dialog.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.util.Log;
import android.view.View;

import androidx.annotation.RequiresApi;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.AttachPopupView;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.enums.PopupAnimation;
import com.lxj.xpopup.enums.PopupPosition;
import com.lxj.xpopup.impl.ConfirmPopupView;
import com.lxj.xpopup.impl.LoadingPopupView;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.lxj.xpopup.interfaces.OnInputConfirmListener;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.lxj.xpopup.interfaces.SimpleCallback;
import com.lxj.xpopup.util.XPermission;

import stark.app.MainActivity;
import stark.app.R;
import stark.app.frame.dialog.custom.CustomAttachPopup;
import stark.app.frame.dialog.custom.CustomAttachPopup2;
import stark.app.frame.dialog.custom.CustomDrawerPopupView;
import stark.app.frame.dialog.custom.CustomEditTextBottomPopup;
import stark.app.frame.dialog.custom.CustomFullScreenPopup;
import stark.app.frame.dialog.custom.PagerBottomPopup;
import stark.app.frame.dialog.custom.PagerDrawerPopup;
import stark.app.frame.dialog.custom.QQMsgPopup;

/**
 * Description:
 * Create by lxj, at 2018/12/11
 */
public class QuickStartDemo extends BaseFragment implements View.OnClickListener {
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_quickstart;
    }

    @Override
    public void init(View view) {
        view.findViewById(R.id.btnShowConfirm).setOnClickListener(this);
        view.findViewById(R.id.btnBindLayout).setOnClickListener(this);
        view.findViewById(R.id.btnShowPosition1).setOnClickListener(this);
        view.findViewById(R.id.btnShowPosition2).setOnClickListener(this);
        view.findViewById(R.id.btnShowInputConfirm).setOnClickListener(this);
        view.findViewById(R.id.btnShowCenterList).setOnClickListener(this);
        view.findViewById(R.id.btnShowCenterListWithCheck).setOnClickListener(this);
        view.findViewById(R.id.btnShowLoading).setOnClickListener(this);
        view.findViewById(R.id.btnShowBottomList).setOnClickListener(this);
        view.findViewById(R.id.btnShowBottomListWithCheck).setOnClickListener(this);
        view.findViewById(R.id.btnShowDrawerLeft).setOnClickListener(this);
        view.findViewById(R.id.btnShowDrawerRight).setOnClickListener(this);
        view.findViewById(R.id.btnCustomBottomPopup).setOnClickListener(this);
        view.findViewById(R.id.btnPagerBottomPopup).setOnClickListener(this);
        view.findViewById(R.id.btnCustomEditPopup).setOnClickListener(this);
        view.findViewById(R.id.btnFullScreenPopup).setOnClickListener(this);
        view.findViewById(R.id.btnAttachPopup1).setOnClickListener(this);
        view.findViewById(R.id.btnAttachPopup2).setOnClickListener(this);
        view.findViewById(R.id.tv1).setOnClickListener(this);
        view.findViewById(R.id.tv2).setOnClickListener(this);
        view.findViewById(R.id.tv3).setOnClickListener(this);
        view.findViewById(R.id.btnMultiPopup).setOnClickListener(this);
        view.findViewById(R.id.btnCoverDialog).setOnClickListener(this);
        view.findViewById(R.id.btnShowInBackground).setOnClickListener(this);

        // 必须在事件发生前，调用这个方法来监视View的触摸
        final XPopup.Builder builder = new XPopup.Builder(getContext())
                .watchView(view.findViewById(R.id.btnShowAttachPoint));
        view.findViewById(R.id.btnShowAttachPoint).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                builder.asAttachList(new String[]{"置顶", "复制", "删除", "编辑编辑编辑编辑编辑编辑编"
                                }, null,
                                new OnSelectListener() {
                                    @Override
                                    public void onSelect(int position, String text) {
                                        toast("click " + text);
                                    }
                                })
                        .show();
                return false;
            }
        });

        drawerPopupView = new CustomDrawerPopupView(getContext());
    }

    CustomDrawerPopupView drawerPopupView;
    AttachPopupView attachPopupView;
    BasePopupView popup;
    ConfirmPopupView popupView;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btnShowConfirm) {
            popupView = new XPopup.Builder(getContext())
                    .setPopupCallback(new SimpleCallback() {
                        @Override
                        public void onCreated(BasePopupView pv) {
                            Log.e("tag", "弹窗创建了");

                            popupView.getContentTextView().setTextColor(Color.RED);
                            popupView.getConfirmTextView().setTextColor(Color.RED);
                        }

                        @Override
                        public void onShow(BasePopupView popupView) {
                            Log.e("tag", "onShow");
                        }

                        @Override
                        public void onDismiss(BasePopupView popupView) {
                            Log.e("tag", "onDismiss");
                        }

                        @Override
                        public void beforeDismiss(BasePopupView popupView) {
                            Log.e("tag", "准备消失的时候执行");
                        }

                        //如果你自己想拦截返回按键事件，则重写这个方法，返回true即可
                        @Override
                        public boolean onBackPressed(BasePopupView popupView) {
                            return true;
                        }
                    }).asConfirm("哈哈", "床前明月光，疑是地上霜；举头望明月，低头思故乡。",
                            "取消", "确定",
                            new OnConfirmListener() {
                                @Override
                                public void onConfirm() {
                                    toast("click confirm");
                                }
                            }, null, false);
            popupView.show();
        } else if (id == R.id.btnBindLayout) {
            new XPopup.Builder(getContext())
                    .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                    .asConfirm("复用项目已有布局", "您可以复用项目已有布局，来使用XPopup强大的交互能力和逻辑封装，弹窗的布局完全由你自己控制。\n" +
                                    "注意：你自己的布局必须提供一些控件Id，否则XPopup找不到View。\n具体需要提供哪些Id，请查看文档[内置弹窗]一章。",
                            "关闭", "XPopup牛逼",
                            new OnConfirmListener() {
                                @Override
                                public void onConfirm() {
                                    toast("click confirm");
                                }
                            }, null, false, R.layout.my_confim_popup) //最后一个参数绑定已有布局
                    .show();
        } else if (id == R.id.btnShowInputConfirm) {
            new XPopup.Builder(getContext())
                    .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                    .autoOpenSoftInput(true)
                    .isRequestFocus(false)
                    .setPopupCallback(new SimpleCallback() {
                        @Override
                        public boolean onBackPressed(BasePopupView popupView) {
                            popupView.dismiss();
                            return true;
                        }
                    })
                    .asInputConfirm("我是标题", null, null, "我是默认Hint文字",
                            new OnInputConfirmListener() {
                                @Override
                                public void onConfirm(String text) {
                                    toast("input text: " + text);
                                }
                            })
                    .show();
        } else if (id == R.id.btnShowCenterList) {
            new XPopup.Builder(getContext())
                    .isDarkTheme(true)
                    .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                    .asCenterList("请选择一项", new String[]{"条目1", "条目2", "条目3", "条目4"},
                            new OnSelectListener() {
                                @Override
                                public void onSelect(int position, String text) {
                                    toast("click " + text);
                                }
                            })
                    .show();
        } else if (id == R.id.btnShowCenterListWithCheck) {
            new XPopup.Builder(getContext())
                    .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                    .asCenterList("请选择一项", new String[]{"条目1", "条目2", "条目3", "条目4"},
                            null, 1,
                            new OnSelectListener() {
                                @Override
                                public void onSelect(int position, String text) {
                                    toast("click " + text);
                                }
                            })
                    .show();
        } else if (id == R.id.btnShowLoading) {
            final LoadingPopupView loadingPopup = (LoadingPopupView) new XPopup.Builder(getContext())
                    .asLoading("正在加载中")
                    .show();
            loadingPopup.postDelayed(new Runnable() {
                @Override
                public void run() {
                    loadingPopup.setTitle("正在加载长度变化了");
                }
            }, 1000);
            loadingPopup.delayDismissWith(3000, new Runnable() {
                @Override
                public void run() {
                    toast("我消失了！！！");
                }
            });
        } else if (id == R.id.btnShowBottomList) {
            new XPopup.Builder(getContext())
                    .isDarkTheme(true)
                    .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                    .asBottomList("请选择一项", new String[]{"条目1", "条目2", "条目3", "条目4", "条目5"},
                            new OnSelectListener() {
                                @Override
                                public void onSelect(int position, String text) {
                                    toast("click " + text);
                                }
                            })
                    .show();
        } else if (id == R.id.btnShowBottomListWithCheck) {
            new XPopup.Builder(getContext())
                    .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                    .asBottomList("", new String[]{"条目1", "条目2", "条目3", "条目4", "条目5"},
                            null, 2,
                            new OnSelectListener() {
                                @Override
                                public void onSelect(int position, String text) {
                                    toast("click " + text);
                                }
                            })
                    .show();
        } else if (id == R.id.btnPagerBottomPopup) {
            new XPopup.Builder(getContext())
                    .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                    .moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面
                    .asCustom(new PagerBottomPopup(getContext()))
                    .show();
        } else if (id == R.id.tv1 ||
                id == R.id.tv2 ||
                id == R.id.tv3) {
            attachPopupView = new XPopup.Builder(getContext())
                    .hasShadowBg(false)
                    .atView(v)  // 依附于所点击的View，内部会自动判断在上方或者下方显示
                    .asAttachList(new String[]{"分享", "编辑编辑编辑编辑编", "不带icon", "分享"},
                            new int[]{R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round},
                            new OnSelectListener() {
                                @Override
                                public void onSelect(int position, String text) {
                                    toast("click " + text);
                                }
                            }, 0, 0);
            attachPopupView.show();
        } else if (id == R.id.btnShowDrawerLeft) {
            new XPopup.Builder(getContext())
                    .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                    .asCustom(new PagerDrawerPopup(getContext()))
                    .show();
        } else if (id == R.id.btnShowDrawerRight) {
            new XPopup.Builder(getContext())
                    .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                    .popupPosition(PopupPosition.Right)//右边
                    .hasStatusBarShadow(true) //启用状态栏阴影
                    .asCustom(drawerPopupView)
                    .show();
        } else if (id == R.id.btnFullScreenPopup) {
            new XPopup.Builder(getContext())
                    .hasStatusBarShadow(true)
                    .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                    .autoOpenSoftInput(true)
                    .asCustom(new CustomFullScreenPopup(getContext()))
                    .show();
        } else if (id == R.id.btnAttachPopup1) {
            new XPopup.Builder(getContext())
                    .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                    .hasShadowBg(false) // 去掉半透明背景
                    .atView(v)
                    .asCustom(new CustomAttachPopup(getContext()))
                    .show();
        } else if (id == R.id.btnAttachPopup2) {
            if (popup == null) {
                popup = new XPopup.Builder(getContext())
                        .atView(v)
                        .hasShadowBg(false) // 去掉半透明背景
                        .asCustom(new CustomAttachPopup2(getContext()));
            }
            popup.show();
        } else if (id == R.id.btnCustomEditPopup) {
            new XPopup.Builder(getContext())
                    .autoOpenSoftInput(true)
                    .asCustom(new CustomEditTextBottomPopup(getContext()))
                    .show();
        } else if (id == R.id.btnShowPosition1) {
            new XPopup.Builder(getContext())
                    .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                    .offsetY(300)
                    .popupAnimation(PopupAnimation.TranslateFromLeft)
                    .asCustom(new QQMsgPopup(getContext()))
                    .show();
        } else if (id == R.id.btnShowPosition2) {
            new XPopup.Builder(getContext())
                    .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                    .isCenterHorizontal(true)
                    .offsetY(200)
                    .asCustom(new QQMsgPopup(getContext()))
                    .show();
        } else if (id == R.id.btnCoverDialog) {
            new AlertDialog.Builder(getContext()).setTitle("我是系统对话框")
                    .setMessage("现在XPopup可以覆盖对话框拉！！！")
                    .setPositiveButton("XPopup弹窗牛逼！", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    }).show();
            delayShow();
        } else if (id == R.id.btnShowInBackground) {
            XPopup.requestOverlayPermission(getContext(), new XPermission.SimpleCallback() {
                @Override
                public void onGranted() {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            new XPopup.Builder(getContext())
                                    .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                                    .enableShowWhenAppBackground(true)  //运行在应用后台弹出
                                    .asConfirm("XPopup牛逼", "XPopup支持直接在后台弹出！", new OnConfirmListener() {
                                        @Override
                                        public void onConfirm() {
                                            startActivity(new Intent(getContext(), MainActivity.class));
                                        }
                                    }).show();
                        }
                    }, 2000);
                }

                @Override
                public void onDenied() {
                }
            });
        }
    }

    public void delayShow() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                new XPopup.Builder(getContext())
                        .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                        .autoDismiss(true).
                        asConfirm("我是XPopup的弹窗", "我可以覆盖系统的Dialog拉！！！",
                                new OnConfirmListener() {
                                    @Override
                                    public void onConfirm() {

                                    }
                                }).show();
            }
        }, 600);
    }


}
